package mspbots.data.tl.prod.autotask.service.impl;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultEntity;
import mspbots.data.tl.prod.autotask.mapper.TaskAutoTaskMapper;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskProject;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskTask;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskTaskService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
@Log4j2
@RequiredArgsConstructor
@DS("master")
public class TaskAutotaskTaskServiceImpl extends AbstractBaseServiceImpl<TaskAutoTaskMapper, TaskAutotaskTask>
        implements TaskAutotaskTaskService {

    private final TaskAutoTaskMapper taskAutoTaskMapper;

    @Override
    public void sync(ResultEntity content) {


        log.debug("Auto Tasks receiver content {}", content);
        JsonNode entity = content.getEntity();
        TaskAutotaskTask data = TaskAutotaskTask.builder()
                .taskId(super.getLongValue(entity.get("id")))
                .status(ObjectUtils.isEmpty(entity.get("status")) ? null : entity.get("status").asText())
                .isTaskBillable(ObjectUtils.isEmpty(entity.get("isTaskBillable")) ? null : entity.get("isTaskBillable").asBoolean())
                .lastActivityDateTime(ObjectUtils.isEmpty(entity.get("lastActivityDateTime")) ? null : LocalDateTime.parse(entity.get("lastActivityDateTime").textValue(), DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .taskType(ObjectUtils.isEmpty(entity.get("taskType")) ? null : entity.get("taskType").asText())
                .completedDateTime(ObjectUtils.isEmpty(entity.get("completedDateTime")) || "null".equalsIgnoreCase(entity.get("completedDateTime").asText())? null : LocalDateTime.parse(entity.get("completedDateTime").textValue(), DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .title(ObjectUtils.isEmpty(entity.get("title")) ? null : entity.get("title").asText())
                .canClientPortalUserCompleteTask(ObjectUtils.isEmpty(entity.get("canClientPortalUserCompleteTask")) ? null : entity.get("canClientPortalUserCompleteTask").asBoolean())
                .createDateTime(ObjectUtils.isEmpty(entity.get("createDateTime")) ? null : LocalDateTime.parse(entity.get("createDateTime").textValue(), DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .departmentId(ObjectUtils.isEmpty(entity.get("departmentID")) ? null : entity.get("departmentID").asLong())
                .description(ObjectUtils.isEmpty(entity.get("description")) ? null : entity.get("description").asText())
                .priority(ObjectUtils.isEmpty(entity.get("priority")) ? null : entity.get("priority").asText())
                .projectId(ObjectUtils.isEmpty(entity.get("projectID")) ? null : entity.get("projectID").asLong())
                .startDateTime(ObjectUtils.isEmpty(entity.get("startDateTime")) || "null".equalsIgnoreCase(entity.get("startDateTime").asText()) ? null : LocalDateTime.parse(entity.get("startDateTime").textValue(), DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .purchaseOrderNumber(ObjectUtils.isEmpty(entity.get("purchaseOrderNumber")) ? null : entity.get("purchaseOrderNumber").asText())
                .taskNumber(ObjectUtils.isEmpty(entity.get("taskNumber")) ? null : entity.get("taskNumber").asText())
                .endDateTime(ObjectUtils.isEmpty(entity.get("endDateTime")) || "null".equalsIgnoreCase(entity.get("endDateTime").asText()) ? null : LocalDateTime.parse(entity.get("endDateTime").textValue(), DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .creatorResourceId(ObjectUtils.isEmpty(entity.get("creatorResourceID")) ? null : entity.get("creatorResourceID").asLong())
                .isVisibleInClientPortal(ObjectUtils.isEmpty(entity.get("isVisibleInClientPortal")) ? null : entity.get("isVisibleInClientPortal").asBoolean())
                .estimatedHours(ObjectUtils.isEmpty(entity.get("estimatedHours")) ? null : entity.get("estimatedHours").asDouble())
                .assignedResourceId(ObjectUtils.isEmpty(entity.get("assignedResourceID")) ? null : entity.get("assignedResourceID").asLong())
                .assignedResourceRoleId(ObjectUtils.isEmpty(entity.get("assignedResourceRoleID")) ? null : entity.get("assignedResourceRoleID").asLong())
                .remainingHours(ObjectUtils.isEmpty(entity.get("remainingHours")) ? null : entity.get("remainingHours").asDouble())
                .phaseId(ObjectUtils.isEmpty(entity.get("phaseID")) ? null : entity.get("phaseID").asLong())
                .syncTime(LocalDateTime.now())
                .build();

        data.setTenantCode(content.getTenantId());


        TaskAutotaskTask dbData = super.baseMapper.selectOne(Wrappers.<TaskAutotaskTask>lambdaQuery()
                .eq(TaskAutotaskTask::getTenantCode, data.getTenantCode())
                .eq(TaskAutotaskTask::getTaskId, data.getTaskId())
                .last("limit 1"));
        if (ObjectUtils.isEmpty(dbData)) {
            super.baseMapper.insert(data);
        } else {
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }

    }
}
